<?php
class interestModel extends appModel{
    
    protected function init(){
        
    } 

    
    public function getSubCats(){       
        $data = array();
        
        $start = isset($_GET['start']) ? $_GET['start'] : 0;
        $limit = isset($_GET['limit']) ? $_GET['limit'] : 10;

        $query = "SELECT * FROM `subject_categories` ORDER BY `cat_name` LIMIT {$start},{$limit}";

        $result = $this->db->query($query);
        
        $nbrows = $this->db->fetchRow($this->db->query("SELECT COUNT(*) FROM `subject_categories`"));
        $nbrows = $nbrows[0];
        while($row=$this->db->fetchAssoc($result)){
            $data[]=$row;
        }
// print_r($data);
        if(MyHelpers::isAjax()){
            // prepare the metadata
            $ret['metaData'] =   $this->makeMetaData($result);
            $ret['total']    =   $nbrows;
            $ret['data']     =   $this->Char2Utf8($data);
            $ret['success']  =   true;
            $ret['message']  =   true;
            return json_encode($ret);
        }

        return $data;       
    }
    public function getyears(){       
        $data = array();
        
        $start = isset($_GET['start']) ? $_GET['start'] : 0;
        $limit = isset($_GET['limit']) ? $_GET['limit'] : 10;

        $query = "SELECT * FROM `year` ORDER BY `sort_order` LIMIT {$start},{$limit}";

        $result = $this->db->query($query);
        
        $nbrows = $this->db->fetchRow($this->db->query("SELECT COUNT(*) FROM `year`"));
        $nbrows = $nbrows[0];
        while($row=$this->db->fetchAssoc($result)){
            $data[]=$row;
        }
// print_r($data);
        if(MyHelpers::isAjax()){
            // prepare the metadata
            $ret['metaData'] =   $this->makeMetaData($result);
            $ret['total']    =   $nbrows;
            $ret['data']     =   $this->Char2Utf8($data);
            $ret['success']  =   true;
            $ret['message']  =   true;
            return json_encode($ret);
        }

        return $data;       
    }
    public function getsemesters(){       
        $data = array();
        
        $start = isset($_GET['start']) ? $_GET['start'] : 0;
        $limit = isset($_GET['limit']) ? $_GET['limit'] : 10;

        $query = "SELECT * FROM `semester` ORDER BY `sort_order` LIMIT {$start},{$limit}";

        $result = $this->db->query($query);
        
        $nbrows = $this->db->fetchRow($this->db->query("SELECT COUNT(*) FROM `sem_name`"));
        $nbrows = $nbrows[0];
        while($row=$this->db->fetchAssoc($result)){
            $data[]=$row;
        }
// print_r($data);
        if(MyHelpers::isAjax()){
            // prepare the metadata
            $ret['metaData'] =   $this->makeMetaData($result);
            $ret['total']    =   $nbrows;
            $ret['data']     =   $this->Char2Utf8($data);
            $ret['success']  =   true;
            $ret['message']  =   true;
            return json_encode($ret);
        }

        return $data;       
    }
    public function getClasses(){       
        $data = array();
        
        $start = isset($_GET['start']) ? $_GET['start'] : 0;
        $limit = isset($_GET['limit']) ? $_GET['limit'] : 10;

        $query = "SELECT * FROM `classes` ORDER BY `class` LIMIT {$start},{$limit}";

        $result = $this->db->query($query);
        
        $nbrows = $this->db->fetchRow($this->db->query("SELECT COUNT(*) FROM `class`"));
        $nbrows = $nbrows[0];
        while($row=$this->db->fetchAssoc($result)){
            $data[]=$row;
        }
// print_r($data);
        if(MyHelpers::isAjax()){
            // prepare the metadata
            $ret['metaData'] =   $this->makeMetaData($result);
            $ret['total']    =   $nbrows;
            $ret['data']     =   $this->Char2Utf8($data);
            $ret['success']  =   true;
            $ret['message']  =   true;
            return json_encode($ret);
        }

        return $data;       
    }

    
    public function getSubjects(){       
        $data = array();
        
        $start = isset($_GET['start']) ? $_GET['start'] : 0;
        $limit = isset($_GET['limit']) ? $_GET['limit'] : 10;

        $query = "SELECT * FROM `subjects` ORDER BY `name` LIMIT {$start},{$limit}";
        $result = $this->db->query($query);
        
        $nbrows = $this->db->fetchRow($this->db->query("SELECT COUNT(*) FROM `name`"));
        $nbrows = $nbrows[0];
        while($row=$this->db->fetchAssoc($result)){
            $data[]=$row;
        }
// print_r($data);
        if(MyHelpers::isAjax()){
            // prepare the metadata
            $ret['metaData'] =   $this->makeMetaData($result);
            $ret['total']    =   $nbrows;
            $ret['data']     =   $this->Char2Utf8($data);
            $ret['success']  =   true;
            $ret['message']  =   true;
            return json_encode($ret);
        }

        return $data;       
    }

    public function getInterest($meta_key){       
        $data = array();
        $query = "SELECT * FROM `user_meta` WHERE user_id=1 and meta_key='masters'  limit 1 ";
        $result = $this->db->query($query);
        $nbrows = $this->db->fetchRow($this->db->query("SELECT COUNT(*) FROM `user_meta`"));
        $nbrows = $nbrows[0];
        while($row=$this->db->fetchAssoc($result)){
            $data[]=$row;
        }

        return $data;       
    }

 public function updateInterest($id=null, $meta_key, $meta_val){

    if($id == null || $id == ""){
        $result = $this->db->query("INSERT INTO user_meta (user_id, meta_key, meta_value) VALUES ('1', '$meta_key', '$meta_val')");
    }else{
        $result = $this->db->query("UPDATE `user_meta` SET `meta_value`='$meta_val' WHERE `id`={$id}");
    }   
        
    if($result !== false){
        $metaid = $this->db->insertId();
    } 
    return $metaid;
    }

}